
package produce

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/produce"
    produceReq "github.com/flipped-aurora/gin-vue-admin/server/model/produce/request"
)

type EquipmentService struct {}
// CreateEquipment 创建设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService) CreateEquipment(ctx context.Context, equipment *produce.Equipment) (err error) {
	err = global.GVA_DB.Create(equipment).Error
	return err
}

// DeleteEquipment 删除设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService)DeleteEquipment(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&produce.Equipment{},"id = ?",id).Error
	return err
}

// DeleteEquipmentByIds 批量删除设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService)DeleteEquipmentByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]produce.Equipment{},"id in ?",ids).Error
	return err
}

// UpdateEquipment 更新设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService)UpdateEquipment(ctx context.Context, equipment produce.Equipment) (err error) {
	err = global.GVA_DB.Model(&produce.Equipment{}).Where("id = ?",equipment.Id).Updates(&equipment).Error
	return err
}

// GetEquipment 根据id获取设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService)GetEquipment(ctx context.Context, id string) (equipment produce.Equipment, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&equipment).Error
	return
}
// GetEquipmentInfoList 分页获取设备记录
// Author [yourname](https://github.com/yourname)
func (equipmentService *EquipmentService)GetEquipmentInfoList(ctx context.Context, info produceReq.EquipmentSearch) (list []produce.Equipment, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&produce.Equipment{})
    var equipments []produce.Equipment
    // 如果有条件搜索 下方会自动创建搜索语句
    
    if info.EquipmentId != nil && *info.EquipmentId != "" {
        db = db.Where("equipment_id LIKE ?", "%"+ *info.EquipmentId+"%")
    }
    if info.EquipmentState != nil && *info.EquipmentState != "" {
        db = db.Where("equipment_state = ?", *info.EquipmentState)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&equipments).Error
	return  equipments, total, err
}
func (equipmentService *EquipmentService)GetEquipmentPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
